package UIFree.controls
{
	import UIFree.controls.scrollbar.uif_scrollHandle;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class uif_scrollbar extends uif_ecomponent {
		public static const UPDATEVALUE: String = 'UPDATEVALUE';
		public static const SCROLLING: String = 'SCROLLING';
		
		public static const DEFAULTSTYLENAME: String = 'scrollbar';
		
		private var _scrollArea	: uif_ecomponent;
		private var _handle		: uif_scrollHandle;
		
		protected var _min		: Number;
		protected var _max		: Number;
		protected var _value		: Number;
		protected var _step		: Number;
		public function uif_scrollbar(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass: Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			var inner: Rectangle = getInner();
			var h4: Number = inner.height / 4;
			_scrollArea = new uif_ecomponent(this, inner.x, inner.y + h4, inner.width, h4, style['barClass'])
			_handle = new uif_scrollHandle(this, inner.x, inner.y, inner.height / 2, inner.height, style['handleClass']);
			addEvent(SCROLLING, doScrolling);
			
			_scrollArea.addEvent(MouseEvent.CLICK, doScrollAreaClick);
			defaultValues();
		}
		
		protected function defaultValues(): void {
			_min 	= 0;
			_max 	= 100;
			_step	= 1;
			value = 0;
		}
		
		public function get min(): Number {
			return _min;
		}
		
		public function set min(a_min: Number): void {
			if (a_min != _min) {
				_min = a_min;
				updatePos();
			}
		}

		public function get max(): Number {
			return _max;
		}

		public function set max(a_max: Number): void {
			if (a_max != _max) {
				_max = a_max;
				updatePos();
			}
		}
		
		public function get value(): Number {
			return _value;
		}
		
		public function get step(): Number {
			return _step;
		}
		
		public function set step(a_step: Number): void {
			if (a_step != _step) {
				_step = a_step;
				updateValue();
			}
		}
		
		public function set value(a_value: Number): void {
			if (a_value != _value) {
				if (a_value < min) a_value = min;
				else if (a_value > max) a_value = max;
				_value = a_value;
				dispatchEvent(new Event(UPDATEVALUE));
				updatePos();
			}
		}
		
		private function doScrolling(e: Event): void {
			updateValue();
		}
		
		private function doScrollAreaClick(e: Event): void {
			value = min + _scrollArea.mouseX/_scrollArea.width * (max - min);
		}
		
		protected function updateValue(): void {
			var inner: Rectangle = getInner();
			var len: Number = max - min;
			var _svalue: Number = (_handle.setX - inner.x) / (inner.width - _handle.width) * len + min;
			value = Math.round(_svalue / _step) * _step;
		}
		
		protected function updatePos(): void {
			var inner: Rectangle = getInner();
			var len: Number = max - min;
			_handle.x = inner.x + (value - min) / len * (inner.width - _handle.width);
		}
		
		public function get handle(): uif_scrollHandle {
			return _handle;
		}
	}
}